package models

import (
	"github.com/beego/beego/v2/client/orm"
	"weiboxs.com/new_h_glasses/utils"
)

//门诊结算
type SaCollectfeesDoctororderMiddle struct {
	Id          int64                     `json:"id"`
	Doctororder *DoDoctorOrder            `orm:"rel(fk);null;on_delete(do_nothing)" json:"doctororder"`
	Collectfees *SaCollectfeesDoctororder `orm:"rel(fk);null;on_delete(do_nothing)" json:"-"`
	TotalMoney  float64                   `json:"total_money"` //总金额
	SumPayable  float64                   `json:"sum_payable"` //应付金额
	CreatedAt   int64                     `json:"created_at"`
	CreatedBy   string                    `json:"created_by"`
}

func (a *SaCollectfeesDoctororderMiddle) TableName() string {
	return SaCollectfeesDoctororderMiddleTableName()
}

func SaCollectfeesDoctororderMiddleTableName() string {
	return TableName("sa_collectfees_doctororder_middle")
}

//查询
type SaCollectfeesDoctororderMiddleQueryParam struct {
	BaseQueryParam
	Filter       string `json:"filter"` //姓名查询
	StartDate    string `json:"start_date"`
	EndDate      string `json:"end_date"`
	DepartmentId int64  `json:"department_id"`
}

//查询列表数据
func SaCollectfeesDoctororderMiddleList(params *SaCollectfeesDoctororderMiddleQueryParam) []*SaCollectfeesDoctororderMiddle {

	query := orm.NewOrm().QueryTable(SaCollectfeesDoctororderMiddleTableName()).RelatedSel("Doctororder", "Doctororder__Archives")
	data := make([]*SaCollectfeesDoctororderMiddle, 0)

	cond := orm.NewCondition();
	if len(params.Filter) > 0 {
		cond1 := orm.NewCondition()
		cond = cond.AndCond(cond1.Or("Doctororder__Archives__pinyin__contains", params.Filter).Or("Doctororder__Archives__realname__contains", params.Filter).Or("Doctororder__Archives__mobile__contains", params.Filter))
	}
	query = query.SetCond(cond)
	if len(params.StartDate) > 0 {
		query = query.Filter("created_at__gte", utils.GetTimeParse(params.StartDate))
	}
	if len(params.EndDate) > 0 {
		query = query.Filter("created_at__lte", utils.GetTimeParse(params.EndDate))
	}
	query = query.Filter("Doctororder__department_id", params.DepartmentId)

	query.OrderBy("-id").All(&data)

	return data
}

//按时间查询数量
func QuerySaCollectfeesDoctororderMiddleCount(depid int64, starttime, endtime int64) int64 {
	query := orm.NewOrm().QueryTable(SaCollectfeesDoctororderMiddleTableName())
	cond := orm.NewCondition()
	cond = cond.AndCond(cond.And("CreatedAt__gte", starttime).And("CreatedAt__lte", endtime))
	query = query.SetCond(cond)
	query = query.Filter("department_id", depid)
	count, _ := query.Count()
	return count
}
